package com.example.demo.common.com.example.demo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class Demo09 {

    public static void main(String[] args) {
        ArrayList<People> personList = new ArrayList<People>();
        personList.add(new People("People1"));
        personList.add(new People("People2"));
        personList.add(new People("People3"));

        printMethod(personList);

        ArrayList<Student> studentList = new ArrayList<Student>();
        // ArrayList<People> a2 = new ArrayList<Student>(); //error 类型安全问题。左右两边要一致
        studentList.add(new Student("student1", 18));
        studentList.add(new Student("student2", 18));
        studentList.add(new Student("student3", 18));
        //printMethod(studentList); error 所需类型和提供类型不匹配
        printMethod2(studentList); // 使用泛型，不能调用具体方法。
        printMethod3(studentList); // 使用通配符，不能调用具体方法。
        printMethod4(studentList);

        TreeSet<People> treeSet = new TreeSet<>();
        treeSet.add(new People("1"));
        treeSet.add(new People("2"));
        treeSet.add(new People("3"));

        MyComparator myComparator = new MyComparator();

        TreeSet<People> treeSet1 = new TreeSet<People>(myComparator);

        TreeSet<Student> treeSet2 = new TreeSet<Student>(myComparator);

        Student student = new Student("朵朵", 18);

        People people = student;


        People people1 = new People("sir");


    }

    // 自定义比较器
    static class MyComparator implements Comparator<People> {
        @Override
        public int compare(People s1, People s2) {
            return s1.getName().compareTo(s2.getName());
        }
    }


    /**
     * 指定类型输出方法，只能输出people集合，不能输出student
     *
     * @param list
     */
    public static void printMethod(ArrayList<People> list) {
        Iterator<People> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next().getName());
        }
    }

    /**
     * 泛型输出方法，people和student集合都能调用，但是由于用的泛型，无法调用对象的具体方法或属性
     *
     * @param list
     * @param <T>
     */
    public static <T> void printMethod2(ArrayList<T> list) {
        for (Iterator<?> it = list.iterator(); it.hasNext(); ) {
            T t = (T) it.next();
            System.out.println(t);
            //System.out.println(t.getName()); // error 找不到属性
        }
    }

    /**
     * 通配符输出方法,people和student集合都能调用，但是由于用的通配符，无法调用对象的具体方法或属性
     *
     * @param list
     */
    public static void printMethod3(ArrayList<?> list) {
        for (Iterator<?> it = list.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
            //System.out.println(it.next().getName()); // error 找不到属性
        }
    }

    /**
     * 使用 ? extends T 为 泛型上限定： 可以接收T和T的子类,people和student集合都能调用,同时能调用父类的属性和方法
     *
     * @param list
     */
    public static void printMethod4(ArrayList<? extends People> list) {
        Iterator<? extends People> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().getName());
        }
    }


    static class All {
        private String sex;

        private String getSex() {
            return this.sex;
        }

        private void setSex(String sex) {
            this.sex = sex;
        }
    }

    static class People extends All {
        public People(String name) {
            this.name = name;
        }

        private String name;

        public String getName() {
            return this.name;
        }
    }

    static class Student extends People {
        Student(String name, Integer age) {
            super(name);
            this.age = age;
        }

        private Integer age;

        public Integer getAge() {
            return this.age;
        }
    }
}
